import Vue from "vue"
import Vuex from "vuex"
import request from "@/http/request";

Vue.use(Vuex);

const store=new Vuex.Store({
    /*存储的用户信息*/
    state: {
        token: window.sessionStorage.getItem("token"),
        //用户信息
        userInfo: null,
        //用户博客信息
        blogInfo: null
    },
    mutations: {
        //登录调用
        setToken(state, token) {
            state.token = token;
            window.sessionStorage.setItem("token", token);
        },
        //登出调用
        removeToken(state) {
            state.token = "";
            state.userInfo = null;
            window.sessionStorage.removeItem("token");
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
        },
        setBlogInfo(state, blogInfo) {
            state.blogInfo = blogInfo;
        }
    },
    actions: {
        getBlogInfo({commit}) {
            request.getBlogInfo().then(res => {
                if (res.code === 0) {
                    commit('setBlogInfo', res.data);
                } else {
                    Notification.error({
                        title: '提示',
                        message: res.msg
                    });
                }
            }).catch(err => {
                console.log(err);
                Notification.error({
                    title: '提示',
                    message: "服务器忙，获取博客信息失败"
                });
            })
        }
    }

});
export default store;